package utils.tabbedpane;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.SwingConstants;
import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.StyleContext;
import utils.code.CodeUtils;


/**
 *
 * @author Juan Carlos García
 */
public class TabbedPaneUtils {
    
    private static String word = "";
    
    /**
     * This method creates a JTabbedPane
     * 
     * @return JTabbedPane
     */
    public static JTabbedPane getTabbedPane(){
        
        JTabbedPane tp = new JTabbedPane();
        return tp;
        
    }
    
    /**
     * This method adds a Tap in a JTabbedPane and adds an element to this new
     * tab.
     * 
     * @param tp JTabbedPane where the system will add the new tab
     * @param fileDetailsTextArea The textEditor 
     */
    public static void addTab(final JTabbedPane tp,String name){
        
        if(!name.equals("Start Page")){
            final StyleContext sc = new StyleContext();
            final DefaultStyledDocument doc = new DefaultStyledDocument(sc);
            JTextPane textArea = new JTextPane(doc);
            tp.add(textArea,tp.getTabCount()); 
            JLabel lbl = new JLabel(name);
            Icon icon = new ImageIcon(TabbedPaneUtils.class.getResource("/resources/img/file.png"));
            lbl.setIcon(icon);
            lbl.setIconTextGap(5);
            lbl.setHorizontalTextPosition(SwingConstants.RIGHT);
            tp.setTabComponentAt(tp.getTabCount() - 1, lbl);

            tp.setSelectedIndex(tp.getTabCount() -1);


            /**
            * fileDetailsTextArea keyListener
            */
            textArea.addKeyListener(new KeyListener(){
                @Override
                public void keyTyped(KeyEvent e) {}

                @Override
                public void keyReleased(KeyEvent e) {}

                @Override
                public void keyPressed(KeyEvent e) {
                   JTextPane textArea = (JTextPane) tp.getComponentAt(tp.getSelectedIndex());
                   if(e.getKeyCode() != 16){
                     checkWord(e.getKeyChar(),textArea,sc,doc);  
                   } 


                }
            });
            
        }else{
            
            
        }
        
        
   
    }
    
    /**
    * Check all the individual words that we write in the JTextPane
    * 
    * @param letter Key tiped by the user
    */
    public static void checkWord(char letter,JTextPane fileDetailsTextArea,StyleContext sc,DefaultStyledDocument doc){

        if(letter == '\b' && word.length() != 0){
            word=word.substring(0,word.length()-1);
        }

        if(letter != ' ' && letter != '('){

          word=word+letter;
        }else{

         CodeUtils.replaceCode(word.trim(),fileDetailsTextArea,fileDetailsTextArea.getCaretPosition(),sc,doc);
         word = "";

        }
    }
    
}